package cz.jm.web.jmlib.client.dialog.registration;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.Widget;

import cz.jm.web.jmlib.client.base.BlockingPresenter;
import cz.jm.web.jmlib.client.base.View;
import cz.jm.web.jmlib.client.dialog.validation.Validable;

public abstract class RegistrationPresenter extends BlockingPresenter {
	
	public interface Display extends View, Validable {
		String getUsername();
		String getPassword();
		String getMail();
		HasClickHandlers getOk();
	}

	private Display view;
	
	public abstract void register(String username, String password, String mail);
	
	public RegistrationPresenter(Display display) {
		view = display;
		
		view.getOk().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {				
				
				if (!view.isValid()) return;
				
				blockApplication();
								
				register(view.getUsername(), view.getPassword(), view.getMail());
															
			}

		});
	}
	
	@Override
    public void go(LayoutPanel workingArea) {
	    final Widget w = view.asWidget();	    
	    addCentredWidget(workingArea, w);
    }
	
}
